package com.oauth.browse.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.oauth.application.client.components.BrowseWindow;
import com.oauth.application.client.rpc.ApplicationAddRemoteService;
import com.oauth.application.client.rpc.ApplicationAddRemoteServiceAsync;
import com.oauth.application.client.rpc.ApplicationRemoteService;
import com.oauth.application.client.rpc.ApplicationRemoteServiceAsync;
import com.oauth.application.client.rpc.BrowseAppRemoteService;
import com.oauth.application.client.rpc.BrowseAppRemoteServiceAsync;
import com.oauth.auth.client.beans.MemberClientBean;
import com.oauth.auth.client.rpc.MemberRemoteService;
import com.oauth.auth.client.rpc.MemberRemoteServiceAsync;
import com.oauth.components.client.application.MyApplications;
import com.oauth.components.client.beans.ApplicationClientBean;
import com.oauth.components.client.rpc.ApplicationUnSubscribeRemoteService;
import com.oauth.components.client.rpc.ApplicationUnSubscribeRemoteServiceAsync;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.footer.FooterUI;
import com.oauth.components.client.ui.header.HeaderUI;
import com.oauth.components.client.ui.windows.GoogleNewsWindow;
import com.oauth.components.client.ui.windows.JoinMemberWindow;
/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BrowseEntryPoint implements EntryPoint {
	
	
	private final ApplicationRemoteServiceAsync applicationService = GWT
	.create(ApplicationRemoteService.class);

	private final MemberRemoteServiceAsync memberService = GWT
	.create(MemberRemoteService.class);

	private final BrowseAppRemoteServiceAsync browseAppService = GWT
			.create(BrowseAppRemoteService.class);
	
	private final ApplicationAddRemoteServiceAsync applicationAddService = GWT
			.create(ApplicationAddRemoteService.class);
	
	private final ApplicationUnSubscribeRemoteServiceAsync applicationRemoveService = GWT
			.create(ApplicationUnSubscribeRemoteService.class);
	
	private static BrowseEntryPointUiBinder uiBinder = GWT
	.create(BrowseEntryPointUiBinder.class);

	interface BrowseEntryPointUiBinder extends UiBinder<DockLayoutPanel, BrowseEntryPoint> {
	}

	@UiField
	HeaderUI header;

	@UiField
	FooterUI footer;
	
	@UiField
	FlowPanel centerPanel;


	@UiField
	VerticalPanel westPanel;

	@UiField
	VerticalPanel eastPanel;

	@UiField
	MyApplications myApplications;
	
	@UiField
	BrowseWindow browseWindow;

	@UiField
	GoogleNewsWindow googleNewsWindow;
	
	@UiField
	JoinMemberWindow joinMemberWindow;
	
	public void onModuleLoad() {
		DockLayoutPanel p = uiBinder.createAndBindUi(this);
		
		memberService.getMember(new OAuthAsnyCallBack<MemberClientBean>() {
							public void onSuccess(MemberClientBean result) {
								header.setUserName(result.getEmail());
							}
		});
		
	    applicationService.getMyApplications(new OAuthAsnyCallBack<ArrayList<ApplicationClientBean>>() {
			public void onSuccess(ArrayList<ApplicationClientBean> result) {
				myApplications.addApplications(result,applicationRemoveService);
			}
		});

	 	
	    browseAppService.getUnSubscribedApplications(new OAuthAsnyCallBack<ArrayList<ApplicationClientBean>>() {
			public void onSuccess(ArrayList<ApplicationClientBean> result) {
			    browseWindow.initBrowseWidget(result,applicationAddService);
			    header.setLoading(false);
			}
		});
	    
	    header.setLoading(false);
		RootLayoutPanel rp = RootLayoutPanel.get();
	    rp.add(p);
		//RootPanel.get().add(dockPanel);
	}

}
